package xjy.smartcommunity.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import xjy.smartcommunity.R;
import xjy.smartcommunity.adapter.MyListViewAdapter;
import xjy.smartcommunity.bean.PropertyVo;
import xjy.smartcommunity.util.HttpUtils;

/**
 * Created by victorzhang on 2017/7/18.
 */

public class WuYeRYActivity extends AppCompatActivity {
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wuye_renyuan);
        Intent intent = getIntent();
        final String idStr = intent.getStringExtra("id");
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                fillDetail(idStr);
            }
        });
        thread.start();
    }

    private void fillDetail(String idStr) {
        String urlStr = getString(R.string.server_host) + "/getBaoXiuYuan.do";
        HashMap<String, String> paraMap = new HashMap<>();
        paraMap.put("id", idStr);
        String errMsg = "";
        Bundle bundle = new Bundle();
        try {
            JSONObject json = HttpUtils.requestJson(this, urlStr, paraMap, false);
            if (json.getInt("status") != 0)
                throw new Exception(json.getString("message"));
            JSONArray arr = json.getJSONArray("data");
            ArrayList<HashMap<String, String>> list = new ArrayList<>();
            for (int i = 0; i < arr.length(); i++) {
                JSONObject jsonObject = arr.getJSONObject(i);
                HashMap<String, String> map = new HashMap<>();
//                map.put("type", jsonObject.getString("type"));
                map.put("name", jsonObject.getString("name"));
                map.put("phone", jsonObject.getString("phone"));
                map.put("id", jsonObject.getString("id"));
                list.add(map);
            }
            bundle.putSerializable("list", list);

        } catch (Exception ex) {
            errMsg = ex.getMessage();
            if (errMsg == null || errMsg.length() == 0)
                errMsg = "请求失败";
        }
        bundle.putString("errMsg", errMsg);
        Message msg = new Message();
        msg.setData(bundle);
        handler.sendMessage(msg);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String errMsg = msg.getData().getString("errMsg");
            if (errMsg.length() > 0) {
                Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_SHORT).show();
                return;
            }
            ListView listView = (ListView) findViewById(R.id.renyuan);
            ArrayList<HashMap<String, String>> listMap = (ArrayList<HashMap<String, String>>) msg.getData().getSerializable("list");
//            SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list, R.layout.listitem_renyuan,
//                    new String[]{"name", "phone"}, new int[]{R.id.RYname, R.id.RYphone});
//            listView.setAdapter(adapter);
            final List<PropertyVo> list = new ArrayList<>();
            for (HashMap<String, String> map : listMap) {
                PropertyVo vo = new PropertyVo();
                vo.setName(map.get("name"));
                vo.setPhone(map.get("phone"));
                list.add(vo);
            }
            MyListViewAdapter adapter = new MyListViewAdapter(list);
//            ListView listView = (ListView) findViewById(R.id.renyuan);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent dialIntent = new Intent(Intent.ACTION_DIAL,
                            Uri.parse("tel:" + list.get(position).getPhone()));
                    WuYeRYActivity.this.startActivity(dialIntent);
                }
            });
        }
    };
}

